/*     */ package com.zimbra.cs.extension;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.servlet.ZimbraServlet;
/*     */ import java.io.IOException;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExtensionDispatcherServlet
/*     */   extends ZimbraServlet
/*     */ {
/*  50 */   private static Map sHandlers = Collections.synchronizedMap(new HashMap());
/*     */   
/*     */ 
/*     */   public static final String EXTENSION_PATH = "/service/extension";
/*     */   
/*     */ 
/*     */ 
/*     */   public static void register(ZimbraExtension ext, ExtensionHttpHandler handler)
/*     */     throws ServiceException
/*     */   {
/*  60 */     handler.init(ext);
/*  61 */     String name = handler.getPath();
/*  62 */     synchronized (sHandlers) {
/*  63 */       if (sHandlers.containsKey(name))
/*  64 */         throw ServiceException.FAILURE("HTTP handler already registered: " + name, null);
/*  65 */       sHandlers.put(name, handler);
/*  66 */       ZimbraLog.extensions.info("registered handler at " + name);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static ExtensionHttpHandler getHandler(String path)
/*     */   {
/*  77 */     ExtensionHttpHandler handler = null;
/*  78 */     int slash = -1;
/*     */     do {
/*  80 */       handler = (ExtensionHttpHandler)sHandlers.get(path);
/*  81 */       if (handler == null) {
/*  82 */         slash = path.lastIndexOf('/');
/*  83 */         if (slash != -1) {
/*  84 */           path = path.substring(0, slash);
/*     */         }
/*     */       }
/*  87 */     } while ((handler == null) && (slash > 0));
/*  88 */     return handler;
/*     */   }
/*     */   
/*     */   public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
/*     */     
/*     */     try {
/*  94 */       ExtensionHttpHandler handler = getHandler(req);
/*  95 */       String method = req.getMethod();
/*  96 */       if ("OPTIONS".equals(method)) {
/*  97 */         handler.doOptions(req, resp);
/*  98 */       } else if ("GET".equals(method)) {
/*  99 */         handler.doGet(req, resp);
/* 100 */       } else if ("POST".equals(method)) {
/* 101 */         handler.doPost(req, resp);
/*     */       } else {
/* 103 */         throw new ServletException("request method " + method + " not supported");
/*     */       }
/*     */     } catch (ServiceException e) {
/* 106 */       throw new ServletException("failed to handle GET request", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static void unregister(ZimbraExtension ext)
/*     */   {
/*     */     Iterator it;
/*     */     
/* 115 */     synchronized (sHandlers) {
/* 116 */       for (it = sHandlers.keySet().iterator(); it.hasNext();) {
/* 117 */         String path = (String)it.next();
/* 118 */         if (path.startsWith("/" + ext.getName())) {
/* 119 */           ExtensionHttpHandler handler = (ExtensionHttpHandler)sHandlers.get(path);
/*     */           try {
/* 121 */             handler.destroy();
/*     */           } catch (Exception e) {
/* 123 */             ZimbraLog.extensions.warn("Error in destroy for handler " + handler.getClass(), e);
/*     */           } finally {
/* 125 */             it.remove();
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private ExtensionHttpHandler getHandler(HttpServletRequest req) throws ServiceException {
/* 133 */     String uri = req.getRequestURI();
/* 134 */     int pos = uri.indexOf("/service/extension");
/* 135 */     String extPath = uri.substring(pos + "/service/extension".length());
/* 136 */     if (extPath.length() == 0)
/* 137 */       throw ServiceException.INVALID_REQUEST("Invalid request: " + uri, null);
/* 138 */     ZimbraLog.extensions.debug("getting handler registered at " + extPath);
/* 139 */     ExtensionHttpHandler handler = getHandler(extPath);
/* 140 */     if (handler == null)
/* 141 */       throw ServiceException.FAILURE("Extension HTTP handler not found at " + extPath, null);
/* 142 */     if (handler.hideFromDefaultPorts()) {
/* 143 */       Server server = Provisioning.getInstance().getLocalServer();
/* 144 */       int port = req.getLocalPort();
/* 145 */       int mailPort = server.getIntAttr("zimbraMailPort", 0);
/* 146 */       int mailSslPort = server.getIntAttr("zimbraMailSSLPort", 0);
/* 147 */       if ((port == mailPort) || (port == mailSslPort))
/* 148 */         throw ServiceException.FAILURE("extension not supported on this port", null);
/*     */     }
/* 150 */     return handler;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/extension/ExtensionDispatcherServlet.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */